package com.mrogrape.notepad.ui.filechooser;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;

import com.mrogrape.system.Environment;


public class OpenOptionPane extends JPanel {

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;


	public OpenOptionPane(){

		this.initializeComponent();
		this.prepare();
	}

	/**
	 * 获取用户选择的字符集
	 * @return
	 */
	public String getCharset(){

		if(this.autoSniffRadio.isSelected()){
			return null;
		}
		else if(this.systemDefaultRadio.isSelected()){
			return System.getProperty("file.encoding");
		}
		else{
			return this.charsetSelector.getSelectedItem().toString();
		}

	}

	private void initializeComponent(){
		this.setPreferredSize(new Dimension(200, 179));

		JPanel charsetPanel = new JPanel();
		charsetPanel.setBorder(new TitledBorder(null, "Charset", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap()
						.addComponent(charsetPanel, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
						.addContainerGap())
		);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap()
						.addComponent(charsetPanel, GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
						.addContainerGap())
		);

		this.autoSniffRadio = new JRadioButton("Auto Sniff");
		this.autoSniffRadio.setSelected(true);
		this.autoSniffRadio.addActionListener(new RaidoChangeListener());
		this.radioButtonGroup.add(this.autoSniffRadio);

		this.systemDefaultRadio = new JRadioButton("System Default");
		this.systemDefaultRadio.addActionListener(new RaidoChangeListener());
		this.radioButtonGroup.add(this.systemDefaultRadio);

		this.specifiedRadio = new JRadioButton("Specified");
		this.specifiedRadio.addActionListener(new RaidoChangeListener());
		this.radioButtonGroup.add(this.specifiedRadio);

		this.charsetSelector = new JComboBox();

		this.systemCharsetView = new JLabel(System.getProperty("file.encoding"));
		this.systemCharsetView.setForeground(Color.GRAY);
		GroupLayout gl_charsetPanel = new GroupLayout(charsetPanel);
		gl_charsetPanel.setHorizontalGroup(
				gl_charsetPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_charsetPanel.createSequentialGroup()
						.addGroup(gl_charsetPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_charsetPanel.createSequentialGroup()
										.addContainerGap()
										.addGroup(gl_charsetPanel.createParallelGroup(Alignment.LEADING)
												.addComponent(this.autoSniffRadio)
												.addComponent(this.specifiedRadio))
												.addGap(6))
												.addGroup(gl_charsetPanel.createSequentialGroup()
														.addGap(27)
														.addComponent(this.charsetSelector, 0, 160, Short.MAX_VALUE))
														.addGroup(gl_charsetPanel.createSequentialGroup()
																.addContainerGap()
																.addGroup(gl_charsetPanel.createParallelGroup(Alignment.LEADING)
																		.addGroup(gl_charsetPanel.createSequentialGroup()
																				.addGap(21)
																				.addComponent(this.systemCharsetView))
																				.addComponent(this.systemDefaultRadio))))
																				.addContainerGap())
		);
		gl_charsetPanel.setVerticalGroup(
				gl_charsetPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_charsetPanel.createSequentialGroup()
						.addContainerGap()
						.addComponent(this.autoSniffRadio)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(this.systemDefaultRadio)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(this.systemCharsetView)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(this.specifiedRadio)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(this.charsetSelector, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(137, Short.MAX_VALUE))
		);
		charsetPanel.setLayout(gl_charsetPanel);
		this.setLayout(groupLayout);
	}

	private void prepare(){

		this.charsetSelector.setEnabled(this.specifiedRadio.isSelected());
	}

	private String[] charsets = null;

	private JRadioButton autoSniffRadio;
	private JRadioButton specifiedRadio;
	private JComboBox charsetSelector;
	private JLabel systemCharsetView;
	private JRadioButton systemDefaultRadio;
	private final ButtonGroup radioButtonGroup = new ButtonGroup();

	private class RaidoChangeListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {

			// 用户是否选择指定编码
			boolean specifiedRadioSelected = OpenOptionPane.this.specifiedRadio.isSelected();

			// 设置编码选择器是否可用
			OpenOptionPane.this.charsetSelector.setEnabled(specifiedRadioSelected);

			if(specifiedRadioSelected && OpenOptionPane.this.charsets == null){

				OpenOptionPane.this.charsets = Environment.availableCharsets();
				OpenOptionPane.this.charsetSelector.setModel(new DefaultComboBoxModel(OpenOptionPane.this.charsets));
				OpenOptionPane.this.charsetSelector.setSelectedItem(OpenOptionPane.this.systemCharsetView.getText());
			}

		}

	}

	public static void main(String args[]){
		JFrame f = new JFrame("test");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().add(new OpenOptionPane());
		f.setVisible(true);
		f.pack();
	}
}
